package com.senven.api.controller;

import com.senven.api.commom.ResponseResult;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSONObject;
import com.senven.api.dao.OrganMapper;
import com.senven.api.dao.OrganUserMapper;
import com.senven.api.pojo.OrganUser;
import com.senven.api.service.SystemService;
import com.senven.api.utils.ReponseResultUtil;
import org.aspectj.bridge.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/system")
public class SystemController {

    private SystemService systemService;

    @PostMapping("/excel/import")
    @ResponseBody
    public ResponseResult importExcel(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        System.out.println(file.isEmpty());
        // 数据处理
        importParams.setHeadRows(0);
        importParams.setTitleRows(0);
        // 需要验证
        importParams.setNeedVerfiy(false);
        System.out.println("进入导入");
        MessageUtil log;
        try {
            //List<OrganUser> userList = ExcelImportUtil.importExcel(file.getInputStream(), OrganUser.class);
            ExcelImportResult<OrganUser> result= ExcelImportUtil.importExcelMore(file.getInputStream(), OrganUser.class, importParams);
            List<OrganUser> userList = result.getList();
            System.out.println(userList.get(1).toString());
            systemService.saveBatch(userList);
        } catch (IOException e) {
            return ReponseResultUtil.success("导入失败");
        } catch (Exception e1) {
            return ReponseResultUtil.success("导入失败");
        }
        return ReponseResultUtil.success("导入成功");
    }

    @GetMapping("/currentUser")
    @ResponseBody
    public Map getCurrUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("account");

        Map result = new HashMap<String,Object>();
        result.put("user",name);
        return result;
    }
}
